/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilclasses;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

/**
 * Utility class just for printing the tickets at the end of the booking
 * @author keaneb1
 */
public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

  /**
   * Method to print out the Java component such as the JPanel in this case
   * @param c the component to be printed.
   */
  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }

  /**
   * 
   * @param componentToBePrinted
   */
  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }

  /**
   * 
   */
  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    PageFormat pageFormat = printJob.pageDialog(printJob.defaultPage());
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }

  /**
   * 
   * @param g
   * @param pageFormat
   * @param pageIndex
   * @return
   */
  @Override
  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      pageFormat.setOrientation(PageFormat.LANDSCAPE);
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

      componentToBePrinted.paint(g2d);
      
      return(PAGE_EXISTS);

    }
  }

}